

#import "CZNetworkManager.h"

@implementation CZNetworkManager

+ (instancetype)sharedManager {
    static CZNetworkManager *instance;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        instance = [self new];
        
        instance.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", nil];
    });
    
    return instance;
}

/// 隔离 AFN 的 GET 方法
///
/// @param urlString  urlString
/// @param parameters 参数字典
/// @param completion 完成回调(json,  error)
- (void)GETRequest:(NSString *)urlString parameters:(NSDictionary *)parameters completion:(void (^)(id json, NSError *error))completion {

    [self GET:urlString parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        completion(responseObject, nil);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"网络请求错误 %@", error);
        
        completion(nil, error);
    }];
}

- (void)PostRequest:(NSString *)urlString parameters:(NSDictionary *)parameters completion:(void (^)(id json, NSError *error))completion {
    
    self.requestSerializer = [AFJSONRequestSerializer serializer];
    
    [self POST:urlString parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        completion(responseObject, nil);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"网络请求错误 %@", error);
    }];
}

#pragma mark - 新闻数据方法
- (void)GetListWithURLString:(NSString *)urlString parameters:(NSDictionary *)parameters completion:(void (^)(id, NSError *))completion {
    
    
    [self GETRequest:urlString parameters:parameters completion:^(id json, NSError *error) {
       
        // 取得第一个 KEY 中的数组
//        NSString *key = json.keyEnumerator.nextObject;
//        NSLog(@"json>>>%@", json);
//        NSArray *array = json;
        
        completion(json, error);
    }];
}

- (void)PostListWithURLString:(NSString *)urlString parameters:(NSDictionary *)parameters completion:(void (^)(id, NSError *))completion {
    
    [self PostRequest:urlString parameters:parameters completion:^(id json, NSError *error) {
        
        //返回个id类型的json具体看服务端提供的数据类型,进行转换
        completion(json, error);
        
    }];
}

- (void)loginWithURLString:(NSString *)urlString parameters:(NSDictionary *)parameters completion:(void (^)(id responseObject))completion {
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", nil];
    
    [manager POST:urlString parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
//        NSLog(@"POST %@ %@ %@", responseObject, [responseObject class], [NSThread currentThread]);
        
        completion(responseObject);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@", error);
    }];
    
}


@end
